#include "scr_state_main_menu.h"
#include "scr_state_credits.h"
#include "scr_state_in_game.h"
#include "scr_game.h"
#include "scr_resource_manager.h"
#include "scr_game_state_manager.h"
#include "scr_texture.h"
#include "scr_font.h"
#include "scr_rect.h"
#include "scr_mouse.h"

#define TITLE_POS_X 40
#define TITLE_POS_Y ((SCRGame::SCR_SCREEN_HEIGHT >> 1) - 40)
#define TITLE_FONT_SIZE 56
#define MENU_POS_X 40
#define MENU_POS_Y ((SCRGame::SCR_SCREEN_HEIGHT / 4 * 3) - 24)
#define MENU_SPACE 48
#define MENU_WIDTH 200
#define MENU_FONT_SIZE 28
#define MENU_OFFSET 8

SCRStateMainMenu::SCRStateMainMenu()
	: m_Time(0)
	, m_SelectedItem(0)
{
}

void SCRStateMainMenu::OnInitialize()
{
	m_Font0 = m_pGame->GetResourceManager()->GetFont("fonts/appleberry_with_cyrillic.ttf", TITLE_FONT_SIZE);
	m_Font1 = m_pGame->GetResourceManager()->GetFont("fonts/carbon.ttf", MENU_FONT_SIZE);
	m_Image = m_pGame->GetResourceManager()->GetTexture("images/main_menu.png");
}

void SCRStateMainMenu::OnUpdate(const int& Delta)
{
	m_Time += Delta;

	if (SCRMouse::IsClicked(SCRMouseButton::LEFT))
	{
		if (1 == m_SelectedItem)
		{
			m_pGame->GetStateManager()->SwitchTo<SCRStateInGame>();
		}
		else if (2 == m_SelectedItem)
		{
			m_pGame->GetStateManager()->SwitchTo<SCRStateCredits>();
		}
		else if (3 == m_SelectedItem)
		{
			m_pGame->GetStateManager()->Pop();
		}
	}
}

void SCRStateMainMenu::OnRender(const int& Delta)
{
	m_Image->Draw(0, 0);
	m_Font0->SetColor(255, 255, 255);
	m_Font0->Draw(TITLE_POS_X, TITLE_POS_Y, "THE SCARLET ROAD", SCRFontType::SOLID, SCRFontAlign::CENTER_Y);

	if (1 == m_SelectedItem)
	{
		m_Font1->SetColor(128, 0, 0);
		m_Font1->Draw(MENU_POS_X, MENU_POS_Y, "Play Game");
		
		m_Font1->SetColor(255, 255, 255);
		m_Font1->Draw(MENU_POS_X - MENU_OFFSET, MENU_POS_Y - MENU_OFFSET, "Play Game");
	}
	else
	{
		m_Font1->SetColor(255, 255, 255);
		m_Font1->Draw(MENU_POS_X, MENU_POS_Y, "Play Game");
	}

	if (2 == m_SelectedItem)
	{
		m_Font1->SetColor(128, 0, 0);
		m_Font1->Draw(MENU_POS_X, MENU_POS_Y + MENU_SPACE, "Credits");

		m_Font1->SetColor(255, 255, 255);
		m_Font1->Draw(MENU_POS_X - MENU_OFFSET, MENU_POS_Y + MENU_SPACE - MENU_OFFSET, "Credits");
	}
	else
	{
		m_Font1->SetColor(255, 255, 255);
		m_Font1->Draw(MENU_POS_X, MENU_POS_Y + MENU_SPACE, "Credits");
	}

	if (3 == m_SelectedItem)
	{
		m_Font1->SetColor(128, 0, 0);
		m_Font1->Draw(MENU_POS_X, MENU_POS_Y + (MENU_SPACE * 2), "Exit Game");

		m_Font1->SetColor(255, 255, 255);
		m_Font1->Draw(MENU_POS_X - MENU_OFFSET, MENU_POS_Y + (MENU_SPACE * 2) - MENU_OFFSET, "Exit Game");
	}
	else
	{
		m_Font1->SetColor(255, 255, 255);
		m_Font1->Draw(MENU_POS_X, MENU_POS_Y + (MENU_SPACE * 2), "Exit Game");
	}
}

void SCRStateMainMenu::OnTerminate()
{
}

void SCRStateMainMenu::OnMouseMove(const int& X, const int& Y)
{
	SCRRect Button0(MENU_POS_X, MENU_POS_Y, MENU_WIDTH, MENU_FONT_SIZE);
	SCRRect Button1(MENU_POS_X, MENU_POS_Y + MENU_SPACE, MENU_WIDTH, MENU_FONT_SIZE);
	SCRRect Button2(MENU_POS_X, MENU_POS_Y + (MENU_SPACE * 2), MENU_WIDTH, MENU_FONT_SIZE);

	if (Button0.Intersect(X, Y))
	{
		m_SelectedItem = 1;
	}
	else if (Button1.Intersect(X, Y))
	{
		m_SelectedItem = 2;
	}
	else if (Button2.Intersect(X, Y))
	{
		m_SelectedItem = 3;
	}
	else
	{
		m_SelectedItem = 0;
	}
}
